﻿#region MIT Licence
/*
 * Copyright (C) 2014 PdfCreative
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace PdfCreative.Pdf.Graphics.Tables
{
    /// <summary>
    /// The Table class represents a table drawn onto a page. Its size is determined by
    /// the rows and columns in the Rows and Columns collections. To alter the structure and
    /// appearance of the table, see the individual Row, Column and Cell instances.
    /// </summary>
    public class Table
    {
        // Private fields
        // ==============
        #region Private fields
        private List<Column> _columns;
        private TableOptions _options;              // The table options.
        private List<Row> _rows;
        private float? _width;                      // The (optional) width of the table.
        #endregion


        
        // Public properties
        // =================
        #region public Properties
        /// <summary>
        /// Retrieves the columns in this table.
        /// </summary>
        public List<Column> Columns
        {
            get { return _columns; }
        }

        /// <summary>
        /// Retrieves the options for this table.
        /// </summary>
        public TableOptions Options
        {
            get { return _options; }
        }

        /// <summary>
        /// Retrieves this table's rows.
        /// </summary>
        public List<Row> Rows
        {
            get { return _rows; }
        }

        /// <summary>
        /// Sets or retrieves the table's optional width. This is a nullable field.
        /// When Width is set to a null reference, the table width is determined by
        /// cell content.
        /// For fixed width, the value must be non-negative.
        /// </summary>
        public float? Width
        {
            get { return _width; }
            set
            {
                if (value.HasValue && value.Value < 0)
                    throw new ArgumentOutOfRangeException(
                        "value", "Width must be zero or a positive number, or a null reference.");

                _width = value;
            }
        }
        #endregion



        // Public methods
        // ==============
        #region Public methods
        /// <summary>
        /// Adds a new column to the table. This method returns the current table instance, so
        /// multiple calls to AddColumn() can be chained together.
        /// </summary>
        /// <param name="column">The column.</param>
        /// <returns>The table instance.</returns>
        /// <exception cref="ArgumentNullException">The column cannot be a Null reference.</exception>
        public Table AppendColumn(Column column)
        {
            if (null == column)
                throw new ArgumentNullException("The column cannot be a Null reference.");

            _columns.Add(column);

            return this;
        }

        /// <summary>
        /// Adds a new fixed-width column to the table. This method returns the current table instance, so
        /// multiple calls to AddColumn() can be chained together.
        /// </summary>
        /// <param name="width">The width of the column. This value must be non-negative.</param>
        /// <returns>The current table instance.</returns>
        public Table AppendColumn(float width)
        {
            return this.AppendColumn(new Column(width));
        }

        /// <summary>
        /// Adds a new variable-width column to the table. This method returns the current table instance, so
        /// multiple calls to AddColumn() can be chained together.
        /// </summary>
        /// <returns>The current table instance.</returns>
        public Table AppendColumn()
        {
            return this.AppendColumn(new Column(null));
        }

        /// <summary>
        /// Adds a new row to the table. This method returns the current table instance.
        /// </summary>
        /// <param name="row">The row.</param>
        /// <returns>This table instance.</returns>
        /// <exception cref="ArgumentNullException">The row cannot be a Null reference.</exception>
        public Table AppendRow(Row row)
        {
            if (null == row)
                throw new ArgumentNullException("The row cannot be a Null reference.");

            _rows.Add(row);

            return this;
        }

        /// <summary>
        /// Adds a new row to the table. This method returns the current table instance, so multiple
        /// calls to AddRow() can be chained together.
        /// </summary>
        /// <param name="height">The height of the row. For a row with a fixed height,
        /// specify a non-negative value. For a row with variable height,
        /// pass a null reference.</param>
        /// <returns>This table instance.</returns>
        public Table AppendRow(float? height)
        {
            if (null != height && height < 0) throw new 
                ArgumentOutOfRangeException("Column height must be non-negative.");

            return this.AppendRow(new Row(height));
        }
        #endregion



        // Public constructor
        // ==================
        #region Public constructor
        /// <summary>
        /// Constructs a new Table instance.
        /// </summary>
        public Table()
        : this(new TableOptions()) { }

        /// <summary>
        /// Constructs a new Table instance with the specified options.
        /// </summary>
        /// <param name="options">The table options that control padding and spacing.</param>
        public Table(TableOptions options)
        {
            _columns = new List<Column>();
            _rows = new List<Row>();

            _options = options;
        }
        #endregion

    } // Table class
}
